package com.hpf.springcloud.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.hpf.springcloud.service.OrderHytrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description:
 * @datetime: 2020/10/4 23:34
 * @author: huangpengfei
 */
@Slf4j
@RestController
//@DefaultProperties(defaultFallback = "paymentTimeoutGlobalFallback")
public class OrderHytrixController {

    @Resource
    private OrderHytrixService orderHytrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentOk(@PathVariable("id") Integer id){
        String s = orderHytrixService.paymentOk(id);
        return s;
    }

//    @HystrixCommand(fallbackMethod = "paymentTimeoutFallback",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "4000")
//    })
//    @HystrixCommand
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentTimeout(@PathVariable("id") Integer id){
        try{
            ThreadUtil.sleep(3000);
        }catch(Exception e){
            e.printStackTrace();
        }
        String s = orderHytrixService.paymentTimeout(id);
        log.info("order**********:{}",s);
        return s;
    }

    /**
     * 兜底的方法
     * @param id
     * @return
     */
    public String paymentTimeoutFallback(Integer id){
        return "80系统发生异常："+ id;
    }

    /**
     * 全局的兜底的方法，配合@DefaultProperties注解使用
     * @return
     */
    public String paymentTimeoutGlobalFallback(){
        return "80系统发生异常全局：";
    }
}
